<?php

class Model_Address {

    public function getAddressById($id) {
        $sql = "SELECT a.info
                FROM address a,site s
                WHERE a.site = s.id
                and a.id = " . $id;
        $db = Model_Connections::getInstance()->getDbConnect();
        $data = $db->fetchAll($sql);
        return $data;
    }

    public function getAddressAllById($id) {
        $sql = "SELECT * FROM address where id = " . $id;
        $db = Model_Connections::getInstance()->getDbConnect();
        $data = $db->fetchAll($sql);
        return $data;
    }

    private function getRealParams($string) {
        return mysql_real_escape_string($string);
    }

    public function updateAddressById($arr) {
        if ($arr != null) {
            $param = array();
            foreach ($arr as $key => $value) {
                $param[$key] = $this->getRealParams($value);
            }

            $model = new Model_Block();
            $blockId = $model->getBlockId($lon, $lat);

            $sql = "UPDATE `address` SET 
            `site` = '" . $param['site'] . "', 
            `info` = N'" . $param['info'] . "',
            `blockId` = '" . $blockId . "',
            `lon` = '" . $param['addressLon'] . "',
            `lat` = '" . $param['addressLat'] . "',
            `description` = N'" . $param['addressDescription'] . "'                    
             WHERE `id` = '" . $param['id'] . "'";
            $db = Model_Connections::getInstance()->getDbConnect();
            return $db->query($sql);
        }
    }

    public function deleteAddress($id) {
        if (isset($id) && $id != null) {
            $sql = sprintf("DELETE FROM `address` WHERE id = '%d' ", mysql_real_escape_string($id));
            $db = Model_Connections::getInstance()->getDbConnect();
            $data = $db->query($sql);
            return $data;
        }
    }

    public function insertAddress($address) {
        if (isset($address) && $address != null) {
            $number = "";
            $siteid = $address['site'];
            $street = "";
            $ward = "";
            $info = $address['info'];
            $lon = $address['addressLon'];
            $lat = $address['addressLat'];
            $description = $address['addressDescription'];
            $db = Model_Connections::getInstance()->getDbConnect();

            $model = new Model_Block();
            $blockId = $model->getBlockId($lon, $lat);

            $query = sprintf("INSERT INTO `address`(`blockId`,`info`,`number`, `site`, `street`,`ward`, `lon`, `lat`, `description`) 
            VALUES ('%s',N'%s','%s','%s',N'%s',N'%s','%s','%s',N'%s')"
                    , mysql_real_escape_string($blockId)
                    , mysql_real_escape_string($info)
                    , mysql_real_escape_string($number)
                    , mysql_real_escape_string($siteid)
                    , mysql_real_escape_string($street)
                    , mysql_real_escape_string($ward)
                    , mysql_real_escape_string($lon)
                    , mysql_real_escape_string($lat)
                    , mysql_real_escape_string($description));


            $data = $db->query($query);
            $query = "Select Max(id) from address";
            $id = $db->fetchAll($query);
            if ($id != null)
                return $id[0]['Max(id)'];
            return null;
        }
    }

}

?>
